<?php

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Model\Category;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class indexcontroller extends commencontroller
{

    //GET.Admin/index分支显示
    function index(){
    $treeresoult=(new Category)->tree();
    return view('Admin.category.index',compact('treeresoult'));
}
    //异步修改
function changeorder(){
      $input=Input::all();
    $cate_id=$input['cateorder_id'];
    $order=$input['order'];
         $upcount=DB::table('category')
             ->where('cate_id',$cate_id)
             ->update(array('cate_order'=>$order));
if ($upcount<=0){
    $date=[
      'status'=>0,
        'msg'=>'修改失败,请稍后再试',
    ];
}else{
    $date=[
        'status'=>1,
        'msg'=>'修改成功',
    ];
}

return $date;


}

    //post. Admin/index
     function store (){
      $input=Input::except('_token','cate_id');

          $rules=['cate_pid'=>'required',

           'cate_name'=>'required'

       ];
          $message=[
              'cate_pid.required'=>'父级分类不能为空',
              'cate_name.required'=>'分类姓名不能为空'
          ];
          $validator=Validator::make($input,$rules,$message);

        if($validator->passes()){
        $adresoult=DB::table('category')
         ->insert(
             $input
         );

    if ($adresoult==true){
      return redirect('Admin/index');
    }else{
        return back()->with('errors','添加失败');
    }

        }else{
            return back()->withErrors($validator);
        }
}


    //GET|HEAD.Admin/index/create添加
    function create (){
       $addresoult=DB::table('category')
            ->where('cate_pid',0)
            ->get();


        return view('Admin/category/add',compact('addresoult'));
  }

    //GET|HEAD.Admin/index/{index}/edit
    function edit ($cate_id){
    $addresoult=DB::table('category')
        ->where('cate_pid',0)
        ->get();
    $upresoult=DB::table('category')
        ->where('cate_id',$cate_id)
        ->get();

    return view('Admin/category/update',compact('upresoult','addresoult'));
}
    //PUT|PATCH.Admin/index/{index
    function update($cate_id){
        $up=Input::except('_token','_method');
      $upcount=  DB::table('category')
          ->where('cate_id',$cate_id)
          ->update(
              $up
          );

      if ($upcount>0){
          return redirect('Admin/index');
      }else{
          return back()->withErrors('errors',修改失败);
      }

    }

    //DELETE Admin/index/{index}
    function destroy($cate_id){
    $delcount=DB::table('category')
        ->where('cate_id',$cate_id)
        ->delete();
    DB::table('category')
        ->where('cate_pid',$cate_id)
        ->update(array(
            'cate_pid'=>0
        ));
    if ($delcount<=0){
        $update=[
            'status'=>0,
            'msg'=>'删除失败,请稍后再试',
        ];
    }else{
        $update=[
            'status'=>1,
            'msg'=>'删除成功',
        ];

    }
    return $update;
}

    //GET|HEAD.Admin/index/{index}
    function show (){

    }


}
